package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.powernode.domain.UserCollection;
import com.powernode.service.UserCollectionService;
import com.powernode.utils.AuthUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户收藏接口管理")
@RestController
@RequestMapping("/p/collection")
public class UserCollectionController {
    @Autowired
    private UserCollectionService userCollectionService;

    @ApiOperation("查询用户收藏商品的数量")
    @GetMapping("/count")
    public ResponseEntity<Integer> loadUserCollectionCount() {
        String userId = AuthUtil.getLoginUserId();
        int count = userCollectionService.count(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, userId));

        return ResponseEntity.ok(count);
    }

    @ApiOperation("查询用户是否收藏了商品")
    @GetMapping("/isCollection")
    public ResponseEntity<Boolean> isCollection(@RequestParam Long prodId) {
        int count = userCollectionService.count(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getProdId, prodId)
                .eq(UserCollection::getUserId, AuthUtil.getLoginUserId())
        );
        return ResponseEntity.ok(count == 1);
    }

    @ApiOperation("取消或确认收藏")
    @PostMapping("/addOrCancel")
    public ResponseEntity<Void> cancelOrAddCollection(@RequestBody Long prodId) {
        String userId = AuthUtil.getLoginUserId();
        userCollectionService.cancelOrAddCollection(userId, prodId);
        return ResponseEntity.ok().build();
    }
}
